<template>
  <div class="school">
    <h2>学校名称: {{ name }}</h2>
    <h2>学校地址: {{ address }}</h2>
    <button @click="sendSchoolMessage">点我传递学校信息</button>
    <button @click="unbindSubscribe">解绑订阅的事件</button>
    <p>要入学的学生信息: {{ studentMessage }}</p>
  </div>
</template>

<script>
// 引入 pubsub 第三方库用于 消息订阅与发布
import pubsub from "pubsub-js";

export default {
  name: "student-name",
  data() {
    return {
      name: "千锋教育",
      address: "北京昌平",
      studentMessage: "",
    };
  },
  methods: {
    sendSchoolMessage() {
      pubsub.publish("getSchool-message", [this.name, this.address]);
    },
    unbindSubscribe() {
      this.$destroy();
    },
  },
  mounted() {
    this.getStudentMessage = pubsub.subscribe(
      "getStudent-message",
      (_, data) => {
        this.studentMessage = data;
      }
    );
  },
  beforeDestroy() {
    console.log(111);
    pubsub.unsubscribe(this.getStudentMessage);
  },
};
</script>

<style scoped>
.school {
  background-color: skyblue;
  padding: 10px;
  margin: 10px;
}
</style>
